﻿// Copyright (C) Jari Hautio <jari.hautio@iki.fi> 2008. Licensed under GPLv2. See LICENSE.txt in solution folder.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace RyijyApp
{
    /// <summary>
    /// Very simple base class for a drawing tool.
    /// Instances of this class are used to route mouse events
    /// to correct tool. Keep DrawingTools in a list and 
    /// hold activeTool in a variable. Then in each Form's event
    /// handler code call appropriate function to trigger tool.
    /// For example within Form1:
    /// public partial class Form1 : Form
    /// {
    ///    DrawingTool currentTool = new DrawingTool();
    ///    public Form1()
    ///    {
    ///        InitializeComponent();
    ///        currentTool.OnClicked += new MouseEventHandler(My_OnClicked);
    ///        // use following line or bind the event in designer
    ///        //this.MouseClick += new MouseEventHandler(Form1_MouseClick);
    ///    }
    ///    void My_OnClicked(object sender, MouseEventArgs e)
    ///    {
    ///        MessageBox.Show("Clicked!");
    ///    }
    ///    void form1_MouseClick(object sender, MouseEventArgs e)
    ///    {
    ///        currentTool.Clicked(sender, e);
    ///    }
    /// }
    /// 
    /// </summary>
    class DrawingTool
    {
        private Cursor cursor;
        private bool active = false;

        public Cursor Cursor
        {
            get { return cursor; }
            set { cursor = value; }
        }
        public event MouseEventHandler OnClicked;
        public void Clicked(object o, MouseEventArgs args)
        {
            if (OnClicked != null)
                OnClicked(o, args);
        }
        public event MouseEventHandler OnMoved;
        public void Moved(object o, MouseEventArgs args)
        {
            if (!active) return;
            if (OnMoved != null)
                OnMoved(o, args);
        }
        public event MouseEventHandler OnDown;
        public void Down(object o, MouseEventArgs args)
        {
            active = true;
            Control c = o as Control;
            if (c != null) c.Capture = false;
            if (OnDown != null)
                OnDown(o, args);
        }
        public event MouseEventHandler OnUp;
        public void Up(object o, MouseEventArgs args)
        {
            Control c = o as Control;
            if (active && c != null) c.Capture = false;
            if (OnUp != null)
                OnUp(o, args);
            active = false;
        }
    };
}
